using System;
using System.Windows;
using System.Windows.Media;
namespace Microsoft.Phone.Controls.Primitives
{
	public class ClipToBounds : DependencyObject
	{
		public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(ClipToBounds), new PropertyMetadata(false, new PropertyChangedCallback(ClipToBounds.OnIsEnabledPropertyChanged)));
		public static bool GetIsEnabled(DependencyObject obj)
		{
			return (bool)obj.GetValue(ClipToBounds.IsEnabledProperty);
		}
		public static void SetIsEnabled(DependencyObject obj, bool value)
		{
			obj.SetValue(ClipToBounds.IsEnabledProperty, value);
		}
		private static void OnIsEnabledPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			FrameworkElement frameworkElement = obj as FrameworkElement;
			if (frameworkElement != null)
			{
				if ((bool)e.get_NewValue())
				{
					frameworkElement.add_SizeChanged(new SizeChangedEventHandler(ClipToBounds.OnSizeChanged));
					return;
				}
				frameworkElement.remove_SizeChanged(new SizeChangedEventHandler(ClipToBounds.OnSizeChanged));
			}
		}
		private static void OnSizeChanged(object sender, SizeChangedEventArgs e)
		{
			FrameworkElement frameworkElement = sender as FrameworkElement;
			if (frameworkElement != null)
			{
				RectangleGeometry rectangleGeometry = new RectangleGeometry();
				rectangleGeometry.set_Rect(new Rect(0.0, 0.0, frameworkElement.get_ActualWidth(), frameworkElement.get_ActualHeight()));
				frameworkElement.set_Clip(rectangleGeometry);
			}
		}
	}
}
